<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once WACT_ROOT . 'validation/rules/match.inc.php';
require_once WACT_ROOT . 'validation/rules/member.inc.php';
require_once WACT_ROOT . 'validation/rules/pattern.inc.php';
require_once APP_ROOT . 'model/common/video.php';
require_once APP_ROOT . 'model/common/categories.php';
require_once APP_ROOT . 'model/common/session.php';
require_once APP_ROOT . 'model/common/form.php';
require_once APP_ROOT . 'model/common/user.php';
require_once APP_ROOT . 'model/common/date.php';
require_once APP_ROOT.'include/inc.config.php';


// video create/update form
class VideoFormController extends FormController {

	var $admin_user = false;
	var $staff_user = false;
	var $view; // current view

	function setUserType($admin_user=false, $staff_user = false) {
		$this->admin_user = $admin_user;
        $this->staff_user = $staff_user;
	}

    function setView(&$view) {
    	$this->view = $view;
    }


	 function VideoFormController() {
     	$this->addChild('create', new ButtonController(new Delegate($this, 'create'))); // create
        $this->addChild('save', new ButtonController(new Delegate($this, 'save'))); // save
        $this->addChild('delete', new ButtonController(new Delegate($this, 'delete'))); // delete

        $this->addChild('clean', new ButtonController(new Delegate($this, 'clean'))); // clean
        $this->addChild('video', new ButtonController(new Delegate($this, 'back'))); // clean

        $this->setDefaultChild('clean');

        // validation
		$this->addRule(new Handle('RequiredRule', array('title')));
        $this->addRule(new Handle('SizeRangeRule', array('title', 112)));


        $this->registerOnLoadListener(new Delegate($this, 'load'));
        //$this->registerOnActivateListener(new Delegate($this, 'check'));

        $this->addView('delete', new Handle('RedirectView', array('/delete')));

	 }





     function checkCategories(&$source, &$request, &$responseModel) {
        // check that two categories are linked
            $category_id = $responseModel->get('category_id');
            $category2_id = $responseModel->get('category2_id');

            //print $category_id.'/'.$category2_id ;

            //if (!$category_id && !$category2_id) {
                //$responseModel->set('msg',"Error: At least one category must be selected!");
            	//return false;
            //}
            if ($category_id && $category2_id) {
            	$linked = CategoriesModel::AreCategoriesLinked($category_id, $category2_id);
            	//print 'result of check '.$linked;
            	if (!$linked) {
                    $responseModel->set('msg',"Error: The categories selected are not linked!");
                    //print $category_id.'/'.$category2_id." Not Linked!";
            		return false;
            	}
            }
            return true;
     }

     function create(&$source, &$request, &$responseModel) {
     	if ($responseModel->isValid()) {
            require_once WACT_ROOT.'/util/phpcompat/clone.php';

            if (!$this->checkCategories($source, $request, $responseModel) ) return 'this';
            $tz =& new MyTimeZone();
            $responseModel->set('date_added', gmdate("Y-m-d H:i", $tz->timeNowLocal() ) );


            $video_id = Video::insertVideo($responseModel);
            if ($video_id) {
            	$responseModel->set('video_id', $video_id);
                $lastRecord = clone_obj($responseModel);


                // Set up the model for adding the next record
                $responseModel->removeAll();
                $responseModel->set('LastRecord', $lastRecord);

                return 'createOK';
            } else {
                return 'failure';
            }
        } else {

        }
     }


	 function delete(&$source, &$request, &$responseModel) {
	 	return 'delete';
	 }

	 function enable(&$source, &$request, &$responseModel) {
        $video_id = $request->getParameter('video_id');
	 	//print 'Activating video:'.$video_id;
	 	if ($video_id) {
	 		Video::publishVideo($video_id);
	 		$responseModel->set('enabled_show', 1);
	 	}

	 	return 'this';
	 }

     function disable(&$source, &$request, &$responseModel) {
        $video_id = $request->getParameter('video_id');
	 	//print 'DeActivating video:'.$video_id;
	 	if ($video_id) {
	 		Video::denyVideo($video_id);
	 		$responseModel->remove('enabled_show');
	 	}

	 	return 'this';
	 }



     function save(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$video_id = $request->getParameter('video_id');


        	if (!$this->checkCategories($source, $request, $responseModel) ) return 'this';


            //prepare admin user
            $admin_user_id = 0;
            if (Session::isLogin()) {
            	$admin_user_id = Session::getUserId();
            }
            if ($admin_user_id) $responseModel->set('admin_user_id',$admin_user_id);


            if ($video_id) Video::updateVideo($video_id, $responseModel);
            return 'saveOK';
        }
    }

    function clean(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'cleanOK';
    }

    function back(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'index';
    }


    function load(&$source, &$request, &$responseModel) {
     	$video_id = $request->getParameter('video_id');
     	//print 'from load: user-id='.$user_id;
     	if ($video_id) {
        	$record =& Video::getRecord($video_id);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());
        }
     }

     //prepare the form view
     function prepare(&$source, &$request, &$responseModel, &$template) {
     	//print 'Prepare form...';
     	$video_id = $request->getParameter('video_id');
     	if ($this->admin_user) {
     		$responseModel->set('admin_user',1);
     	}
     	if ($this->staff_user) $responseModel->set('staff_user',1);
        $responseModel->set('host',HOST);

        $responseModel->set('enabled',1);

		// determine max custom order for new video tracks
		if (!$video_id) {
        	$maxOrder =& Video::getVideoMaxCustomOrder();
        	$responseModel->set('order_number',$maxOrder+10);
        }

        // determine the current user
        if (Session::isLogin()) {
        	$user_id = Session::getUserId();
        	$isAuthor = User::isUserArticleWriter($user_id);
        	$isAdmin =  User::isUserAdmin($user_id);
        }

        if ($isAuthor) $responseModel->set('author_user',1);

     	//populate categories
        if ($isAuthor)  $categories1 =& CategoriesModel::getSportCategoriesForAuthor($user_id);
        else  $categories1 =& CategoriesModel::getSportCategories(false,false,true);
        //print_r($categories1);
        $categories2 =& CategoriesModel::getCommunityCategories(false,false,true);
        $category1 =& $template->getChild('category_id');
        $category2 =& $template->getChild('category2_id');
        $category1->setChoices($categories1);
        $category2->setChoices($categories2);

     }



}

?>